package com.example.myapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapp.R;
import com.example.myapp.entity.ItemBean;
import com.example.myapp.entity.SolarTermBean;

import java.util.List;
//RecyclerView列表适配器-加载网络链接图片，在此之前一定要在AndroidManifest.xml添加网络权限请求 <uses-permission android:name="android.permission.INTERNET"/>

//Adapter从模型层获取数据，然后提供给RecyclerView显示，是沟通的桥梁。
public class MyRecyclerViewNetDataAdapter extends RecyclerView.Adapter<MyRecyclerViewNetDataAdapter.MyViewHolder>{

    //上下文
    private Context mContext;
    //列表数据
    private List<SolarTermBean> mList;
    //
    private  LayoutInflater inflater;

    public MyRecyclerViewNetDataAdapter(Context context){
        this.mContext=context;
        //创建LayoutInflater对象
        inflater= LayoutInflater.from(mContext);
    }

    public void setList(List<SolarTermBean> mList) {
        this.mList = mList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

      //这个方法主要生成为每个Item inflater出一个View，
        //如果root不为null，attachToRoot设为false，则会将布局文件最外层的所有layout属性进行设置，当该view被添加到父view当中时，这些layout属性会自动生效。
        View view = inflater.inflate(R.layout.list_item_layout,parent,false);
        //创建ViewHolder
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        //获取item的数据对象
        SolarTermBean solarTermBean = mList.get(position);
        //获取item的空间实例
        ImageView iconImg = holder.icon_img; //图片控件
        TextView titleTv = holder.title_tv;//标题文本控件
        TextView contentTv = holder.content_tv; //内容文本控件
        RelativeLayout itemRoot = holder.item_root; //item根布局，为点击item做准备
        //设置标题值
        titleTv.setText(solarTermBean.getTitle());
        //设置item内容值
        contentTv.setText(solarTermBean.getContent());
        //使用Glide加载图片到图片控件
        // this：上下文   url：要加载的图片链接   imageView: 要显示的图片控件
        Glide.with(mContext).load(solarTermBean.getImageUrl()).into(iconImg);
        //点击监听
        itemRoot.setOnClickListener((v)->{ //点击监听
            Toast.makeText(mContext,"点击"+mList.get(position).getTitle(),Toast.LENGTH_SHORT).show();
        });



//        holder.icon_img.setImageResource(mList.get(position).getImageInt());
//        holder.title_tv.setText(mList.get(position).getTitle());
//        holder.content_tv.setText(mList.get(position).getContent());
//        holder.item_root.setOnClickListener((v)->{ //点击监听
//            Toast.makeText(mContext,"点击"+mList.get(position).getTitle(),Toast.LENGTH_SHORT).show();
//        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
  //创建ViewHolder和将模型层的数据绑 定到ViewHolder上
    class  MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView icon_img;
        private TextView title_tv;
        private TextView content_tv;
        private RelativeLayout item_root;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            icon_img= itemView.findViewById(R.id.icon_img);
            title_tv=itemView.findViewById(R.id.title_tv);
            content_tv= itemView.findViewById(R.id.content_tv);
            item_root=itemView.findViewById(R.id.item_root);
        }
    }


}
